﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace crdmcgui
{
    public class Restraints
    {
        public string Version;

        public string[] AllowedKeys;
        public string[] AllowedValues;

        public string[] NumericKeys;
        public string[] PsuedoNumericKeys;
        public string[] ListKeys;
        public string[] YesNoKeys;
        public string[] MangaYesNoKeys;
        public string[] LanguageISOKeys;
        //public string[] DateTimeKeysFields;

        public string[] AllowedKeyModifiers;
        public string[] AllowedValueModifiers;


        string[] numericalKeysModifiers;
        string[] ListKeyModifiers;
        string[] LanguageISOKeyModifiers;
        string[] YesNoKeyModifiers;
        string[] MangaYesNoKeyModifiers;
        //string[] DateTimeKeyModifiers;





        string[] NumericValueModifiers;
        string[] ListValueModifiers;
        string[] LanguageISOValueModifiers;
        string[] YesNoValueModifiers;
        string[] MangaYesNoValueModifiers;
        //string[] DateTimeValueModifiers;



        public Restraints(string strDataManIniFilePath)
        {
            if (System.IO.File.Exists(strDataManIniFilePath))
            {
                string[] iniSettings = System.IO.File.ReadAllLines(strDataManIniFilePath);

                for (int i = 0; i < iniSettings.Length; i++)
                {
                    string line = iniSettings[i];
                    if (!line.StartsWith(";"))
                    {
                        string[] tmp = line.Split(new string[] { " = ", "=" }, 2, StringSplitOptions.RemoveEmptyEntries);
                        if (tmp.Length > 1)
                        {
                            string strKey = tmp[0];
                            string strValue = tmp[1];

                            switch (strKey.ToLower())
                            {
                                case "version":
                                    this.Version = strValue;
                                    break;
                                case "allowedkeys":
                                    this.AllowedKeys = strValue.Split(new string[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
                                    break;
                                case "languageisokeys":
                                    this.LanguageISOKeys = strValue.Split(new string[] { " , ", "," }, StringSplitOptions.RemoveEmptyEntries);
                                    break;
                                case "numericalkeys":
                                    this.NumericKeys = strValue.Split(new string[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
                                    break;
                                case "multivaluekeys":
                                    this.ListKeys = strValue.Split(new string[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
                                    break;
                                case "pseudonumericalkeys":
                                    this.PsuedoNumericKeys = strValue.Split(new string[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
                                    break;
                                case "yesnokeys":
                                    this.YesNoKeys = strValue.Split(new string[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
                                    break;
                                case "mangayesnokeys":
                                    this.MangaYesNoKeys = strValue.Split(new string[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
                                    break;
                                case "allowedkeymodifiers":
                                    this.AllowedKeyModifiers = strValue.Split(new string[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
                                    break;
                                case "allowedkeymodifiersmulti":
                                    this.ListKeyModifiers = strValue.Split(new string[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
                                    break;
                                case "allowedkeymodifiersnumeric":
                                    this.numericalKeysModifiers = strValue.Split(new string[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
                                    break;
                                case "allowedkeymodifierlanguageiso":
                                    this.LanguageISOKeyModifiers = strValue.Split(new string[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
                                    break;
                                case "allowedkeymodifieryesno":
                                    this.YesNoKeyModifiers = strValue.Split(new string[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
                                    break;
                                case "allowedkeymodifiermangayesno":
                                    this.MangaYesNoKeyModifiers = strValue.Split(new string[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
                                    break;
                                case "allowedvals":
                                    this.AllowedValues = strValue.Split(new string[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
                                    break;
                                case "allowedvalmodifiersnumeric":
                                    this.NumericValueModifiers = strValue.Split(new string[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
                                    break;
                                case "allowedvalmodifiers":
                                    this.AllowedValueModifiers = strValue.Split(new string[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
                                    break;
                                case "allowedvalmodifiersmulti":
                                    this.ListValueModifiers = strValue.Split(new string[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
                                    break;
                                case "allowedvalmodifierlanguageiso":
                                    this.LanguageISOValueModifiers = strValue.Split(new string[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
                                    break;
                                case "allowedvalmodifieryesno":
                                    this.YesNoValueModifiers = strValue.Split(new string[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
                                    break;
                                case "allowedvalmodifiermangayesno":
                                    this.MangaYesNoValueModifiers = strValue.Split(new string[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
                                    break;
                            }
                        }
                    }
                }
            }
        }

        public string[] GetAllowedKeyModifiers(string strAllowedKey)
        {
            List<string> arrAllowedModifiers = new List<string>(AllowedKeyModifiers);
            arrAllowedModifiers.RemoveAll(n => n.StartsWith("Great") || n.StartsWith("Less"));

            if (this.NumericKeys.Contains(strAllowedKey) || this.PsuedoNumericKeys.Contains(strAllowedKey))
                arrAllowedModifiers = new List<string>(numericalKeysModifiers);
            else if (this.ListKeys.Contains(strAllowedKey))
                arrAllowedModifiers = new List<string>(ListKeyModifiers);
            else if (this.LanguageISOKeys.Contains(strAllowedKey))
                arrAllowedModifiers = new List<string>(this.LanguageISOKeyModifiers);
            else if (this.YesNoKeys.Contains(strAllowedKey))
                arrAllowedModifiers = new List<string>(this.YesNoKeyModifiers);
            else if (this.MangaYesNoKeys.Contains(strAllowedKey))
                arrAllowedModifiers = new List<string>(this.MangaYesNoKeyModifiers);


            return arrAllowedModifiers.ToArray();
        }

        public string[] GetAllowedValueActions(string strAllowedKey)
        {
            List<string> arrAllowedModifiers = new List<string>(AllowedValueModifiers);

            if (this.NumericKeys.Contains(strAllowedKey) || this.PsuedoNumericKeys.Contains(strAllowedKey))
                arrAllowedModifiers = new List<string>(new string[] { "SetValue", "Calc" });
            else if (this.ListKeys.Contains(strAllowedKey))
                arrAllowedModifiers = new List<string>(ListValueModifiers);
            else if (this.LanguageISOKeys.Contains(strAllowedKey))
                arrAllowedModifiers = new List<string>(this.LanguageISOValueModifiers);
            else if (this.YesNoKeys.Contains(strAllowedKey))
                arrAllowedModifiers = new List<string>(this.YesNoValueModifiers);
            else if (this.MangaYesNoKeys.Contains(strAllowedKey))
                arrAllowedModifiers = new List<string>(this.MangaYesNoValueModifiers);

            return arrAllowedModifiers.ToArray();
        }

        public string[] GetMangaYesNoValues()
        {
            return new string[] { "", "No", "Yes", "YesAndRightToLeft" };
        }

        public string[] GetYesNoValues()
        {
            return new string[] { "", "Yes", "No" };
        }

        public SortedDictionary<string, string> GetAllLanguages()
        {
            SortedDictionary<string, string> EnglishNameTwoLetterISO = new SortedDictionary<string, string>();

            foreach (CultureInfo item in CultureInfo.GetCultures(CultureTypes.NeutralCultures))
            {
                EnglishNameTwoLetterISO.Add(item.EnglishName, item.TwoLetterISOLanguageName);
            }

            return EnglishNameTwoLetterISO;
        }
    }
}
